package com.example.prom.dangwen.y2022.controller;

import com.alibaba.fastjson.JSON;
import com.example.prom.dangwen.y2022.model.OrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("dw/y2022/")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("test")
    public void test() throws InterruptedException {
        Random random = new Random();
        List<String> areaCodeList = Arrays.asList("86", "94", "65", "43", "21", "10");
        List<String> templateList = Arrays.asList("okx_template", "okcoin_template", "oklink_template", "other_template");
        // 模拟一千次请求
        for (int i = 0; i < 1000; i++) {
            // 模拟请求间隔
            int sleepTime = random.nextInt(3000);
            int areaCodeIndex = random.nextInt(6);
            int templateIndex = random.nextInt(4);
            System.out.println("睡眠："+sleepTime+" 毫秒");
            Thread.sleep(sleepTime);
            int nextInt = random.nextInt(100);
            System.out.println("随机数为："+nextInt);
            String orderUrl = "http://localhost:9091/order";
            OrderModel orderModel = new OrderModel();
            orderModel.setUserId(1L);
            orderModel.setAmount(3);
            orderModel.setOrderId("orderId_1");
            orderModel.setAreacode(areaCodeList.get(areaCodeIndex));
            orderModel.setTemplate(templateList.get(templateIndex));
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            // 模拟随机调用渠道
            if (nextInt < 10){
                orderModel.setChannel("sw,mw,jm,twilio");
            } else if (nextInt < 40){
                orderModel.setChannel("sw,mw");
            } else if (nextInt < 50){
                orderModel.setChannel("mw,jm");
            } else if (nextInt < 90){
                orderModel.setChannel("jm,twilio");
            } else {
                orderModel.setChannel("twilio");
            }
            HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(orderModel), httpHeaders);
            restTemplate.postForObject(orderUrl, entity, Object.class);
        }
    }
}
